package com.atkxp.web;

import com.atkxp.domain.Book;
import com.atkxp.domain.Page;
import com.atkxp.service.BookService;
import com.atkxp.service.impl.BookServiceImpl;
import com.atkxp.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author user
 * @declaration
 * @create 2022/9/27-19:38
 */
@WebServlet(name = "ClientBookServlet", value = "/client/bookServlet")
public class ClientBookServlet extends BaseServlet {
    private BookService bookService = new BookServiceImpl();
    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum;
        int pageSize;
        if(request.getParameter("pageNum") == null){
            pageNum = 1;
        }else{
            pageNum = Integer.parseInt(request.getParameter("pageNum"));
        }

        if(request.getParameter("pageSize") == null){
            pageSize = Page.PAGE_SIZE;
        }else{
            pageSize = Integer.parseInt(request.getParameter("pageSize"));
        }

        Page<Book> page = bookService.page(pageNum, pageSize);
        page.setUrl("client/bookServlet?action=page");

        request.setAttribute("page", page);

        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }
    protected void pageByPrice(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum = WebUtils.parseInt(request.getParameter("pageNum"), 1);
        int pageSize = WebUtils.parseInt(request.getParameter("pageSize"), Page.PAGE_SIZE);
        int max = WebUtils.parseInt(request.getParameter("max"), 0x3f3f3f3f);
        int min = WebUtils.parseInt(request.getParameter("min"), 0);
        Page<Book> page = bookService.pageByPrice(pageNum, pageSize, min, max);
        page.setUrl("client/bookServlet?action=pageByPrice&min=" + min + "&max=" + max);

        request.setAttribute("page", page);

        request.getRequestDispatcher("/pages/client/index.jsp").forward(request, response);
    }
}
